package www.xudan.android_ffmpeg.videoplayer.widget;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import www.xudan.android_ffmpeg.R;
import www.xudan.android_ffmpeg.videoplayer.util.FullScreenUtils;
import www.xudan.android_ffmpeg.videoplayer.util.Px2Dp;


public class GeneralPlayerView extends InitByOtherPlayerView {

    public GeneralPlayerView(Context context) {
        super(context);
    }

    public GeneralPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GeneralPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    FullScreenListener fullScreenListener;
    ViewGroup parent;
    ViewGroup.LayoutParams lp;

    @Override
    protected void initView() {
        super.initView();
        full_screen.setImageResource(R.drawable.full);
        full_screen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fullScreen();
            }
        });
        ll_set_by_touch.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ll_set_by_touch.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ViewGroup.LayoutParams layoutParams = ll_set_by_touch.getLayoutParams();
                layoutParams.height = Px2Dp.dip2px(getContext(), 90);
                layoutParams.width = Px2Dp.dip2px(getContext(), 180);
                ll_set_by_touch.setLayoutParams(layoutParams);
            }
        });

    }

    @Override
    public void fullScreen() {
        int videoOrientation = FullScreenUtils.getVideoOrientation(player);
        ViewGroup contentView = activity.findViewById(android.R.id.content);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        parent = (ViewGroup) this.getParent();
        parent.removeView(this);
        lp = this.getLayoutParams();
        contentView.addView(this, layoutParams);
        if (videoOrientation != FullScreenUtils.VIDEO_ORIENTATION_LENGTHWAYS) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }
        full_screen.setImageResource(R.drawable.exit_full);
        full_screen.setOnClickListener(v -> exitFullScreen());
        if (fullScreenListener != null) {
            fullScreenListener.fullScreen();
        }
    }

    @Override
    public void exitFullScreen() {
        ViewGroup contentView = activity.findViewById(android.R.id.content);
        contentView.removeView(this);
        full_screen.setImageResource(R.drawable.full);
        full_screen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fullScreen();
            }
        });
        parent.addView(this, lp);
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        if (fullScreenListener != null) {
            fullScreenListener.exitFullScreen();
        }
    }

    public void setFullScreenListener(FullScreenListener fullScreenListener) {
        this.fullScreenListener = fullScreenListener;
    }

    public interface FullScreenListener {
        void fullScreen();

        void exitFullScreen();
    }

}
